/** 
  导师库维护
*/
<template>
  <div>
      <condition-card @reset="reset" @search="onSubmit" label-position="right" label-width="150px">
        <el-row :gutter="24" style="height: 50px;">
         
          <el-col :span="8">
              <el-form-item :label="$t('mainbase.affiliatedUnit')">
                <el-select   v-model="queryForm.condition.department" filterable clearable style="width: 100%">
                  <el-option
                      v-for="item in formsLearn"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                  </el-option>
                </el-select> 
                <!-- <el-input v-model="queryForm.condition.department"></el-input> -->
              </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
              <el-form-item :label="$t('mainbase.gender')">
                <DictSelect v-model="queryForm.condition.sex" filterable :data="options.sex" :plzSelect="true"></DictSelect>
              </el-form-item>
          </el-col>
          <el-col :span="8">
              <el-form-item :label="$t('mainbase.titleTechnical')">
                <DictSelect v-model="queryForm.condition.proTitle" filterable @change="changeClick" :data="options.title" :plzSelect="true"></DictSelect>
                  
              </el-form-item>
          </el-col> -->
          <el-col :span="8">
            <el-form-item :label="$t('mainbase.status')">
              <el-select v-model="queryForm.condition.isEnabled" clearable style="width: 100%">
                <el-option label="启用" :value="1"> </el-option>
                <el-option label="停用" :value="0"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          
        </el-row>
      </condition-card>
      <el-card shadow="never" v-loading="loading" body-style="padding: 24px;" class="card-margin">
        <h1 class="condition-title">{{$t('mainbase.libraryList')}}</h1>
        <div class="float-left associated-conditional-input">
          <KeywordInput v-model="queryForm.condition.name" :placeholder="$t('mainbase.pleasesSearch')"
            @enter="onSubmit"/>
        </div>
        <div class="edu-opertion-bar sub-title">
          <div class="float-right" style="display: flex;">
            <el-dropdown placement='bottom-start' trigger="click" @command="leadFile">
              <el-button type="primary" v-if='leadBut'>{{$t('internship.lead')}}<i class="el-icon-arrow-down el-icon--right"></i></el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="导入">{{$t('internship.lead')}}</el-dropdown-item>
                <el-dropdown-item command="下载">{{$t('internship.download')}}</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
            <el-upload 
              class="upload-demo"
              action="/api/practiceservice/specialist/import" 
              :data="category"
              :auto-upload="true"
              :on-success="changeSuccess"
              style="display: none;"
              >
              <span slot="trigger" ref="importBtn">{{$t('graduate.clickUpload')}}</span>
              <!-- <el-button size="small" ref='importBtn' type="primary">点击上传</el-button> -->
              <!-- :on-preview="handlePreview"
              :on-remove="handleRemove"
              :before-remove="beforeRemove"
              :on-exceed="handleExceed"
              :file-list="fileList"<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div> -->
            </el-upload>
            <!-- <el-dropdown style="margin-left:5px" placement='bottom-start' trigger="click" @command="exportFile">
              <el-button type="primary">{{$t('internship.derive')}}<i class="el-icon-arrow-down el-icon--right"></i></el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="exportPDF1">{{$t('entry.exportPDF')}}</el-dropdown-item>
                <el-dropdown-item command="exportWord1">{{$t('entry.exportWord')}}</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown> -->
            <el-button style="margin-left:10px" type="primary" @click="exportFile" v-if='deriveBut'>{{$t('internship.derive')}}</el-button>
            <el-button type="primary" @click="exportAdd" v-if='addBut'>{{$t('graduate.add')}}</el-button>
            <!-- <el-button @click="exportEdit">{{$t('graduate.edit')}}</el-button>
            <el-button @click="exportServe">{{$t('graduate.conserve')}}</el-button>
            <el-button type="primary" @click="addedLine">{{$t('internship.addedLine')}}</el-button>
            <el-button type="danger" @click="delline">{{$t('internship.delline')}}</el-button> -->
          </div>
        </div>
        <edu-table :pagination="false"  :tooltipEffect="tooltipEffect" stripe :data="dataList" size="mini" @selection-change="handleSelectionChange">
        
          <edu-table-column :label="$t('mainbase.name')" prop="name"></edu-table-column>
          <edu-table-column :label="$t('mainbase.gender')" prop="sex">
            <template slot-scope="props">
              <span v-if="props.row.sex == '2'">女</span>
              <span v-if="props.row.sex == '1'">男</span>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('mainbase.age')" prop="age"></edu-table-column>
          <edu-table-column :label="$t('mainbase.affiliatedUnit')" prop="department"></edu-table-column>
          <edu-table-column :label="$t('mainbase.titleTechnical')" prop="proTitle"></edu-table-column>
          <edu-table-column :label="$t('mainbase.instructors')" prop="introduction"></edu-table-column>
          <edu-table-column :label="$t('mainbase.status')" prop="isEnabled">
            <template slot-scope="scope">
              <div class="is-enabled-container">
                <el-button v-if="scope.row.isEnabled === 0" type="danger" plain>{{ scope.row.isEnabled | isEnabled }}</el-button>
                <el-button v-else type="success" plain>{{ scope.row.isEnabled | isEnabled }}</el-button>
              </div>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('mainbase.account')" prop="account"></edu-table-column>
          <edu-table-column :label="$t('mainbase.initial')" prop="password"></edu-table-column>
          
          <edu-table-column :label="$t('graduate.operate')" fit width="150">
            <template slot-scope="props">
              
              <!-- <el-button type="text" @click="checkOutline(props.row)" >{{$t('internship.check')}}</el-button> -->
              <!-- <el-button type="text" @click="specialEdit(props.row)" >{{$t('internship.specialEdit')}}</el-button> -->
              <el-button v-if='editBut' type="text" @click="editOutline(props.row)" >{{$t('graduate.edit')}}</el-button>
              <el-button v-if='detailsBut' type="text" @click="detailsOutline(props.row)" >{{$t('graduate.details')}}</el-button>
              <!-- <el-button type="text" @click="deteOutline(props.row)" >{{$t('internship.dete')}}</el-button> -->
             
            </template>
          </edu-table-column>
        </edu-table>
        <el-row type="flex" class="row-bg" justify="end">
          <el-col>
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryForm.pageNum_"
                :page-sizes="[20, 40, 80, 100]"
                :page-size="queryForm.pageSize_"
                layout="total, sizes, prev, pager, next, jumper"
                :total="queryForm.total_"
                style="float:right;margin-top:10px"
              ></el-pagination>
          </el-col>
        </el-row>
      </el-card>
      <el-dialog
        @close="dataObjit={}"
        :before-close='beforeClose'
        :title="titleList"
        width="70%"
        :visible.sync="addflag"
        :show-close="beforeClose">
        <libraryMaintAdd ref='addform' @cancel-dialog='changeClose' :dataQueryForm='dataQueryForm' :showFlag="addflag" :dataProp='dataProp' :dataList='mainbase' :dataListProp='dataObjit' 
        @close="(flag) => { addflag = false }" @affirm-form="addrmForm" >
        </libraryMaintAdd>
      </el-dialog>
      <el-dialog
        @close="tableClose"
        :title="$t('mainbase.library')"
        width="70%"
        :before-close='tableClose'
        :visible.sync="tableFlag"
        :show-close="tableClose">
        <libraryMaintView :showFlag="tableFlag"  :dataListProp='dataObjit' :dataList='mainbase' @close="(flag) => { tableFlag = flag }"></libraryMaintView>
      </el-dialog>
      <edu-export-dialog
        :disabled="exportButton"
        :title="$t('message.prompt')"
        ref="downloadRef"
        :data="exportResult">
      </edu-export-dialog>
  </div>
</template>
<script>
import { queryDic } from "common/src/api/dictionary";
import UTILS from "common/src/utils/utils";
import libraryMaintView from 'common/src/views/graduateStudent/component/libraryMaintView';
// import addDialog from 'common/src/views/graduateStudent/component/EduTable';
import libraryMaintAdd from 'common/src/views/graduateStudent/component/libraryMaintAdd';
import graduateStudent from 'common/src/api/graduateStudent/graduateStudent';
// import SchoolCalendarApi from "common/src/api/calendars";
// import graduateOuline from "./graduateOuline";
// import detailsTempList from "./detailsTempList";
export default {
  components: {
    libraryMaintView,
    libraryMaintAdd
  },
  data () {
    return {
      indexKey: '1', // 面包屑
      title: '',
      titleList: '',
      category: { category: 2 },
      mainbase: '导师',
      showDate: true,
      addflag: false,
      tableFlag: false,
      peopleadd: {},
      curRow: {},
      dataQueryForm: {},
      ifDetials: false,
      copyDialog: false,
      curCalendarId: '',
      isAllDel: true,
      exportButton: false,
      exportResult: null,
      checkTopicListForm: {
        dataList: []
      },
      multipleSelection: [],
      provideData: {}, // 提供子组件表格数据
      queryForm: {
        pageNum_: 1,
        pageSize_: 20,
        total_: 0,
        condition:{
          category: 2,
          department: '',
          isEnabled: ''
        }
      },
      formsLearn: [],
      id: '',
      dataListProp: {},
      dataProp: '',
      copyTime: {
        time: ''
      },
      options: {
        facultys: [],
        timeList: [],
        title: [],
        sex: [],
        auditStatus: [
          {
            label: '待提交',
            value: 0
          },
          {
            label: '待审核',
            value: 1
          },
          {
            label: '审核通过',
            value: 2
          },
          {
            label: '驳回',
            value: 3
          },
          {
            label: '待录入',
            value: 4
          }
        ]
      },
      addBut: false,
      editBut: false,
      inquireBut: false,
      deriveBut: false,
      leadBut: false,
      detailsBut: false,
      queryList: {
        calendarId: '',
        faculty: '',
        status: '',
        course: ''
      },
      rules: {
       
      },
      rulesForm: {
       
      },
      dataList: [],
      breadPath: {
        label: this.$t('graduate.graduate'),
        key: "1",
        children: [
          {
            key: "2",
            label: ""
          }
        ]
      },
    }
  },
  watch: {
    'auths': {
      handler: function(value) {
        value.some(item => {
          if (item.authCode === 'libraryMaintenance-addBut') {
            // 新增
            this.addBut = true;
          }
          if (item.authCode === 'libraryMaintenance-editBut') {
            // 编辑
            this.editBut = true;
          }
          if (item.authCode === 'libraryMaintenance-inquireBut') {
            // 查询
            this.inquireBut = true;
          }
          if (item.authCode === 'libraryMaintenance-deriveBut') {
            // 导出
            this.deriveBut = true;
          }
          if (item.authCode === 'libraryMaintenance-leadBut') {
            // 导入
            this.leadBut = true;
          }
          if (item.authCode === 'libraryMaintenance-detailsBut') {
            // 详情
            this.detailsBut = true;
          }
        });
      },
      deep: true
    }
  },
  filters: {
    isEnabled(val) {
      switch (val) {
        case 0:
          return '停用'
        case 1:
          return '启用'
        default:
          return val
      }
    },
  },
  created() {
    this.auths = JSON.parse(localStorage.getItem("sessiondata")).auths;
    this.auths.some(item => {
      if (item.authCode === 'libraryMaintenance-addBut') {
        // 新增
        this.addBut = true;
      }
      if (item.authCode === 'libraryMaintenance-editBut') {
        // 编辑
        this.editBut = true;
      }
      if (item.authCode === 'libraryMaintenance-inquireBut') {
        // 查询
        this.inquireBut = true;
      }
      if (item.authCode === 'libraryMaintenance-deriveBut') {
        // 导出
        this.deriveBut = true;
      }
      if (item.authCode === 'libraryMaintenance-leadBut') {
        // 导入
        this.leadBut = true;
      }
      if (item.authCode === 'libraryMaintenance-detailsBut') {
        // 详情
        this.detailsBut = true;
      }
    });
    this.postApproveAgree(this.queryForm);
  },
  mounted() {
    this.getOptions();
    this.getSessionBase();
  },
  methods: {
    postApproveAgree(date) {
      graduateStudent.approveAgree(date).then(res => {
        // this.queryForm.pageNum_ = res.data.pageNum_;
        // this.queryForm.pageSize_ = res.data.pageSize_;
        this.queryForm.total_ = res.data.total_;
        this.dataList = res.data.list;
      });
    },
    getSessionBase() {
      graduateStudent.getSession().then(res => {
        this.id = res.data.realUid;
      }).then(() => {
        this.getBaseListByBase(this.id);
      });
    },
    getBaseListByBase(id) {
      this.formsLearn = [];
      graduateStudent.getBaseListByBaseAccount(id).then(res => {
        // console.log(res, 'fsfs');
        if (res.data.length > 0) {

          res.data.forEach(el => {
            this.formsLearn.push({
              label: el.baseName,
              value: el.baseName
            }) 
          });
          // this.queryForm.condition.department = res.data[0].baseName;
        }
      });
    },
    getOptions() {
      // 筛选条件字典查询
      let data = {
        lang: this.$store.getters.language || "cn",
        type: "allChild",
        keys: ["profession", 'G_XBIE']
      };
      queryDic(data).then(res => {
        UTILS.fillSelect(
          {
            title: "profession",
            sex:"G_XBIE"
          },
          res.data,
          this.options
        );
      });
    },
    changeClick(val) {
      this.options.title.forEach(el => {
        if (val === el.value) {
          this.queryForm.condition.proTitle = el.label;
        }
      });
    },
    // 导入
    leadFile(command) {
      if (command === '导入') {
        this.channel = true;
        this.$refs.importBtn.click();
      } else if (command === '下载') {
        graduateStudent.downloadLibrary().then(res => {
          this.$message(this.$t('internship.successfully'));
        });
      }
    },
    changeSuccess(res) {
      if(res.msg){
        this.$message({type: "info", message: res.msg});
      }else{
        // this.$message(this.$t('internship.successfully'));
        this.$message({type: "success", message: this.$t('internship.successfully')});
        this.queryForm.pageNum_ = 1;
        this.postApproveAgree(this.queryForm);
        // this.$message({type: "success", message: this.$t('message.addSuccess')});
      }
    },
    // 轮询直到获取key值的接口返回
    cycleToGetKey() {
      setTimeout(() => {
        let currentIds = [];
        if (this.multipleSelection.length > 0) {
          this.multipleSelection.forEach((element) => {
            currentIds.push(element.id);
          });
        } else {
         currentIds = [];
        }
        graduateStudent.specialist({
          "templateName": this.queryForm.condition.department,
          // "baseLevel": this.queryForm.condition.proTitle,
          "isEnabled": this.queryForm.condition.isEnabled,
          // "name": this.queryForm.condition.name,
          // "sex": this.queryForm.condition.sex,
          "category": this.queryForm.condition.category,
          "selectRowIds": currentIds
        }).then(res => {
          if (res.success) {
            this.$refs.downloadRef.close();
            this.exportButton = false;
            this.normalDelayRequest(res.key, res.msg);
          } else {
            this.cycleToGetKey();
          }
        });
      }, 3000);
    },
    // 导出异步
    delayRequest(key, name) {
      setTimeout(() => {
        graduateStudent.downloadGetKey(key, name).then(res => {
          this.exportResult = res.data;
          if (res.code == 200 && res.data.status) {
            this.exportButton = false;
            let path =
              '/api/practiceservice/export/file?path=' +
              res.data.path + '&name=' + name;
            // window.open(path);
            window.location.href = path;
            this.$message.success(this.$t('result.exportSuccessfully'));
            this.$refs.downloadRef.close();
          } else {
            this.delayRequest(key, name);
          }
        });
      }, 3000);
    },
    // Normal导出异步
    normalDelayRequest(key, name) {
      setTimeout(() => {
        graduateStudent.downloadGetKey(key).then(res => {
          this.exportResult = res.data;
          if (res.code == 200 && res.data.status) {
            this.exportButton = false;
            let path =
              '/api/practiceservice/export/file?path=' +
              res.data.path + '&name=' + name;
            // window.open(path);
            window.location.href = path;
            this.$message.success(this.$t('result.exportSuccessfully'));
            this.$refs.downloadRef.close();
          } else {
            this.delayRequest(key, name);
          }
        });
      }, 3000);
    },
    // 导出
    exportFile(command) {
      // 1、第一步：调该接口，获取key值以及msg中的文件名
      // 2、第二步：将key值放入接口传参
      // 3、第三步：传入path值以及name(msg中的文件名)值，下载文件
      this.exportResult = null;
      this.$refs.downloadRef.open();
      this.exportButton = true;
      this.cycleToGetKey();
      
    },
    // 编辑
    exportEdit() {
      this.showDate = false;
    },
    // 搜索/筛选
    onSubmit() {
      this.queryForm.pageNum_ = 1;
      this.postApproveAgree(this.queryForm);
    },
    // 新增
    exportAdd() {
      this.title = this.$t('graduate.add');
      this.dataProp = '新增';
      this.mainbase = '导师';
      this.titleList = this.$t('graduate.add');
      this.dataQueryForm = this.queryForm;
      this.addflag = true;
    },
    // 取消
    changeClose(param) {
      this.addflag = param;
    },
    // 取消
    beforeClose(param) {
      this.$refs.addform.cancelDialog();
    },
    tableClose() {
      this.tableFlag = false;
    },
    // 新增保存（编辑保存）
    addrmForm(param, data) {
      if (this.mainbase === '导师' && this.dataProp === '编辑') {
        graduateStudent.maintenanceEidt(param).then(res => {
          this.$message.success(this.$t('internship.successfully')); 
          this.addflag = false;
          // this.queryForm.pageNum_ = 1;
          this.postApproveAgree(data);
          this.$refs.addform.confirmInner();
        });
      } else if (this.mainbase === '导师' && this.dataProp === '新增') {
        graduateStudent.maintenanceAdd(param).then(res => {
          this.$message.success(this.$t('internship.successfully')); 
          
          this.addflag = false;
          // this.queryForm.pageNum_ = 1;
          this.postApproveAgree(data);
          this.$refs.addform.confirmInner();
        });
      }
      
    },
    addCancel(data) {
      this.addflag = data;
    },
    
    getListData() {
      undergraduategraduateApi.getgraduateList(this.queryForm).then((res) => {
        this.dataList = res.data.list;
        this.queryForm.total_ = res.data.total_;
      })
    },
    initOptions() {
      innovativeApi.getAllFaculty().then(res => {
        this.options.facultys = res.data.map(item => {
          return {
            label: item.deptName,
            value: item.deptCode
          }
        });
      })
    },
    // async getCurrentTermCalendar(){
    //   return await SchoolCalendarApi.currentTermCalendar();
    // },
    handleSelectionChange(val) {
      this.multipleSelection = val;
     
    },
    
    // 编辑
    editOutline(row) {
      this.title = this.$t('graduate.edit');
      this.dataProp = '编辑';
      this.mainbase = '导师';
      this.titleList = this.$t('graduate.edit');
      this.dataObjit = row;
      this.dataQueryForm = this.queryForm;
      this.addflag = true;
    },
    // 详情
    detailsOutline(row) {
      this.dataObjit = row;
      this.mainbase = '导师';
      this.tableFlag = true;
    },
    // 关闭弹框
    changeProp() {
      this.addflag = false;
    },
    // 查看考核指标表
    indicatorsList() {},
    // 重置
    reset() {
      for (let key in this.queryForm.condition) {
        if (key !== 'category') {
          this.queryForm.condition[key] = '';
        }
      }
      this.queryForm.pageNum_ = 1;
      this.postApproveAgree(this.queryForm);
    },
    
    // 保存
    conserve() {
      this.addflag = false;
    },
   
    diaCancel() {
      this.$refs['copyTime'].resetFields();
      this.copyDialog = false;
    },
    
    dialogClose() {
      this.copyDialog = false;
      this.$refs['copyTime'].resetFields();
    },
    
    handleSizeChange(val) {
      this.queryForm.pageSize_ = val;
      this.queryForm.pageNum_ = 1;
      this.postApproveAgree(this.queryForm);
    },
    handleCurrentChange(val) {
      this.queryForm.pageNum_ = val;
      this.postApproveAgree(this.queryForm);
    }
    
  }
  
}
</script>
<style lang="scss" scoped>
.is-enabled-container ::v-deep {
    .el-button {
      cursor: auto;
    }
    .el-button--danger {
      padding: 9px 15px;
      min-width: 0;
    }
}
.condition-title {
  color: #2b3b4e;
  font-size: 18px;
  line-height: 24px;
  font-weight: bold;
}
  .edu-page-title {
    span {
      line-height: 24px;
      display: inline-block;
      padding-right: 10px;
      font-size: 18px;
      font-weight: bold;
      color: #2b3b4e;
    }
  }
  .sub-title {
    margin: 16px 0;
    font-size: 16px;
    color: #000020;
    letter-spacing: 0;
  }
  .form-item {
    width: 312px;
    height: 32px;
    float: left;
    margin: 0px 10px;
  }
  .inputBox{
    width: 240px!important;height: 32px!important;
  }
  .atooltip{
    background: beige;
    border: black;
  }
</style>
